# -*- coding: iso-8859-2 -*-

from authorshipanalyzer.textutils import Splitter
from authorshipanalyzer.slot import Filler
from authorshipanalyzer.clp import ClpUtils
from authorshipanalyzer.slot import Slot

class SexFiller(Filler):
    '''
    Class which recognize sex of the writer
    and fill slot with recognize value
    '''
    
    def __init__(self):
        self.splitter = Splitter()
        self.clpUtils = ClpUtils()
    
    def fill(self, slot):
        '''
        Discover and set sex of the writer
        @param slot: slot to fill
        '''
        sText = self.splitter.createWordList(slot.message.getText(), False, False)
        for word in sText:
            #True - map word to the diacritic one
            sex = self.clpUtils.getAuthorSex(word, True)
            if sex != "unknown":
                slot.sex = sex
                return
#            
#        slot.setSex("unknown")
                
if __name__ == "__main__":
    import datetime
    import os
    
    from authorshipanalyzer.message import Message
    from authorshipanalyzer.message import MessageReader
    from authorshipanalyzer.file import Path
    from authorshipanalyzer.slot import Slot
    from time import time

    fromDate = datetime.date(1004, 4, 1)
    toDate = datetime.date(3004, 4, 22)
    
    mr = MessageReader(Path.getMainDir() + os.sep + "info1")
    messages = mr.readMessages(fromDate, toDate)
             
    start = time()
               
    recognizedSex = 0.0
    man = 0.0
    woman = 0.0
    sf = SexFiller()
    for message in messages:
        s = Slot()
        s.setMessage(message)
        sf.fill(s)
        if s.getSex() != "unknown":
#            print message.getAuthor(), s.getSex()
            recognizedSex += 1.0
            if s.getSex() == "woman":
                woman += 1.0
            if s.getSex() == "man":
                man += 1.0
    end = time()
          
    print "Number of processed messages:", len(messages)        
    print "Sex recognized in " + str(recognizedSex / len(messages) * 100) + "%"
    print "Woman: " + str(woman / len(messages) * 100) + "%"
    print "Man: " + str(man / len(messages) * 100) + "%"
    print "Processing lasts for: " + str(end - start) + " seconds"
        
        